package com.itheima.controller;

import com.itheima.pojo.Tag;
import com.itheima.service.BlogTagService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.TagVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 博客标签相关接口
 */
@RestController
@RequestMapping("/tag")
@Api(tags = "博客标签相关接口")
@CrossOrigin
public class BlogTagController {
    @Autowired
    BlogTagService blogTagService;

    //添加标签
    @PostMapping("/add")
    @ApiOperation("添加标签")
    public String add(@RequestBody TagVO tagVO){
        try{
            blogTagService.add(tagVO);
            return ResultUtil.result("success", MessageConstant.TAG_ADD_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_ADD_FAIL);
        }
    }
    //通过引用量排序标签
    @PostMapping("/tagByCite")
    @ApiOperation("通过引用量排序标签")
    public String TagByCite(){
        try {
            List<Tag> TagList = blogTagService.tagSortByCite();
            return ResultUtil.result("success",TagList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_BY_CITE_FAIL);
        }
    }

    //通过点击量排序标签
    @PostMapping("/tagByClickCount")
    @ApiOperation("通过点击量排序标签")
    public String TagByClickCount(){
        try {
            List<Tag> TagList=blogTagService.tagSortByClickCount();
            return ResultUtil.result("success",TagList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_BY_CLICK_COUNT_FAIL);
        }
    }

    //批量删除标签
    @PostMapping("/deleteBatch")
    @ApiOperation("批量删除标签")
    public String deleteBatch(@RequestBody TagVO tagVO){
        if (tagVO.getUid()==null||"".equals(tagVO.getUid())){
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,"请选择需要删除的标签");
        }
        try {
            blogTagService.deleteBatch(tagVO);
            return ResultUtil.result("success",MessageConstant.TAG_DELETE_BATCH_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_DELETE_BATCH_FAIL);
        }
    }

    //编辑标签
    @PostMapping("/edit")
    @ApiOperation("编辑标签")
    public String edit(@RequestBody TagVO tagVO){
        if (tagVO.getUid()==null||"".equals(tagVO.getUid())){
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,"请选择需要编辑的标签");
        }
        try {
            blogTagService.edit(tagVO);
            return ResultUtil.result("success",MessageConstant.TAG_EDIT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_EDIT_FAIL);
        }
    }

    //获取标签列表
    @PostMapping("/getList")
    @ApiOperation("获取标签列表")
    public String edigetListt(@RequestBody TagVO tagVO){
        try {
            List<Tag> TagList= blogTagService.getList(tagVO);
            return ResultUtil.result("success",TagList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_FINDALL_FAIL);
        }
    }

    //置顶分类
    @PostMapping("/stick")
    @ApiOperation("置顶分类")
    public String stick(@RequestBody TagVO tagVO){
        if (tagVO.getUid()==null||"".equals(tagVO.getUid())){
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST,"请选择需要置顶的标签");
        }
        try {
            List<Tag> tagList=blogTagService.stick(tagVO);
            return ResultUtil.result("success",tagList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.TAG_DELETE_BATCH_FAIL);
        }
    }

}
